package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.DpBaseDataDictionaryMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.ext.DpBaseDataDictionaryExt;
import com.zjdiepu.www.util.CachedEnums;

/**
 * 数据字典Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月06日 16时04分<br/>
 * @since 1.0<br/>
 */
@Service
public class DpBaseDataDictionaryServiceImpl extends BaseService implements IDpBaseDataDictionaryService {
	public static final Logger logger = LoggerFactory.getLogger(DpBaseDataDictionaryServiceImpl.class);

	@Autowired
	private DpBaseDataDictionaryMapper dpBaseDataDictionaryMapper;
	@Autowired
	private ICachedDictionaryService cachedDictionaryService;
	
	@Override
	public void saveUpdateAndDelete(DpBaseDataDictionary dpBaseDataDictionary, OperateType operType) {
		try {
			switch (operType) {
			case add:
				dpBaseDataDictionary.initInsertData(dpBaseDataDictionary.getModifier(), dpBaseDataDictionary.getModifierName());
				dpBaseDataDictionaryMapper.insert(dpBaseDataDictionary);
				break;
			case edit:
				dpBaseDataDictionary.initUpdateData(dpBaseDataDictionary.getModifier(), dpBaseDataDictionary.getModifierName());
				// 影响的记录数
				int updateCount = dpBaseDataDictionaryMapper.updateByPrimaryKeySelective(dpBaseDataDictionary);
				if(updateCount == 0){
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} else if(updateCount > 1){
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
				break;
			case delete:
				dpBaseDataDictionary.initDeleteData(dpBaseDataDictionary.getModifier(), dpBaseDataDictionary.getModifierName());
				// 影响的记录数
				int deleteCount = dpBaseDataDictionaryMapper.updateByPrimaryKeySelective(dpBaseDataDictionary);
				if(deleteCount == 0){
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} else if(deleteCount > 1){
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
				break;
			default:
				break;
			}
		} finally {
			DataDicType dataDicType = CachedEnums.getByCode(dpBaseDataDictionary.getParentValue(), DataDicType.class);
			cachedDictionaryService.cachedAndRefreshDataDics(dataDicType, true);
		}
	}
	
	@Override
	public List<DpBaseDataDictionary> selectByExt(DpBaseDataDictionaryExt dpBaseDataDictionaryExt) {
		return dpBaseDataDictionaryMapper.selectByExt(dpBaseDataDictionaryExt);
	}
	
	@Override
	public int countByExt(DpBaseDataDictionaryExt dpBaseDataDictionaryExt){
		return dpBaseDataDictionaryMapper.countByExt(dpBaseDataDictionaryExt);
	}
	
	@Override
	public DpBaseDataDictionary selectById(String id) {
		return dpBaseDataDictionaryMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dpBaseDataDictionaryMapper.clearEmpty(fieldNames, id);
		}
	}
}
